/**************************************************************************
 *  Copyright(C) Armin Moradi feng.shaun@gmail.com                        *
 *                                                                        *
 *  This file is part of CGAddress.                                       *
 *                                                                        *
 *  CGAddress is free software: you can redistribute it and/or modify     *
 *  it under the terms of the GNU General Public License as published by  *
 *  the Free Software Foundation, either version 3 of the License, or     *
 *  (at your option) any later version.                                   *
 *                                                                        *
 *  CGAddress is distributed in the hope that it will be useful,          *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *  GNU General Public License for more details.                          *
 *                                                                        *
 *  You should have received a copy of the GNU General Public License     *
 *  along with CGAddress.  If not, see <http://www.gnu.org/licenses/>.    *
 **************************************************************************/

#include "mainwindow.h"

#include <QString>
#include <QDebug>

MainWindow::MainWindow() : QMainWindow(0) {
	qDebug() << "MainWindow::MainWindow()";
	
	setupUi(this);
	setupConnections();
}

MainWindow::~MainWindow() {
	qDebug() << "MainWindow::~MainWindow()";
}

void MainWindow::setupConnections() {
	// connect(sender, SIGNAL(signalname(args)), receiver, SLOT(slotname(args)))
	qDebug() << "MainWindow::setupConnections()";
	
	connect(addButton, SIGNAL(clicked()), this, SLOT(addName()));
	connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteName()));
	connect(okButton, SIGNAL(clicked()), this, SLOT(close()));
	connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
}

void MainWindow::addName() {
	qDebug() << "MainWindow::addName()";
	
	QString text = lineEdit->text();
	if (text.trimmed().isEmpty()) {
		lineEdit->clear();
		return;
	}
	listWidget->addItem(text);
	lineEdit->clear();
}

void MainWindow::deleteName() {
	qDebug() << "MainWindow::deleteName()";

	QListWidgetItem *item = listWidget->takeItem(listWidget->currentRow());
	//process item before deleting
	delete item;
}

